package com.service.admin.impl;

import com.dao.UserDao;
import com.service.admin.AdminUserService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 管理员关于用户的服务实现类
 *
 * 
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

  @Override
  public int getAllUserCount() {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserDao dao = session.getMapper(UserDao.class);
    int count = dao.selectAllUserCount();
    session.close();
    return count;
  }

  @Override
  public List<Map<String, Object>> getAllUsers(int pageSize, int pageIndex, String userId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserDao dao = session.getMapper(UserDao.class);
    List<Map<String, Object>> users = dao.selectAllUsers(pageSize * pageIndex, pageSize, userId);
    session.close();
    return users;
  }

  @Override
  public Map<String, Object> getUserCommentPermission(String userId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserDao dao = session.getMapper(UserDao.class);
    Map<String, Object> userCommentPermission = dao.selectUserCommentPermission(userId);
    session.close();
    return userCommentPermission;
  }

  @Override
  public int setUserSellPermission(String userId, Date toTime, int goodBeReportedCount) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserDao dao = session.getMapper(UserDao.class);
    int row = dao.updateSellPermission(userId, toTime, goodBeReportedCount);
    session.commit();
    session.close();
    return row;
  }

  @Override
  public Map<String, Object> getUserGoodPermission(String userId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserDao dao = session.getMapper(UserDao.class);
    Map<String, Object> userGoodPermission = dao.selectSellPermission(userId);
    session.close();
    return userGoodPermission;
  }

  @Override
  public int setUserCommentPermission(String userId, Date toTime, int commentBeReportCount) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    UserDao dao = session.getMapper(UserDao.class);
    int row = dao.updateCommentPermission(userId, toTime, commentBeReportCount);
    session.commit();
    session.close();
    return row;
  }
}
